# -*- coding: UTF-8 -*-
# 描述数据卖家，其采样时刻预先确定
import random

COST = 1  # 获取数据成本
DELTA = 1  # 价格递减速度


class SellerAoISP:
    def __init__(self, id, qbuyer, T, l=-1, price=[], updates=[]):
        self.id = id  # index
        self.w = []  # 向其发送报价的买家
        self.flag = True  # 本轮是否有改变？
        self.delta = 0  # 边缘AoI单元开销
        # 给各个买家的报价
        if (len(price) == 0):
            self.price = [random.randint(10, 20) for _ in range(qbuyer)]
        else:
            self.price = price
        # 更新次数
        if(l < 0):
            self.l = random.randint(1, 10)
        else:
            self.l = l
        self.cost = COST*self.l  # 成本
        # 具体的更新时间
        if(len(updates) == 0):
            self.updates = getRanUpdate(self.l, T)
        else:
            self.updates = updates

    # 寻找所有报价高于成本的买家
    def findBuyerSet(self, b):
        self.w = []
        for i in range(len(b)):
            if (self.price[i] > self.cost):
                self.w.append(i)

    # 调整（降低）价格
    def adjustPrice(self, bindex):
        self.price[bindex] = max(self.price[bindex] - DELTA, self.cost)


def getRanUpdate(l, T):
    """获取随机的采样时间
    """
    u = [random.randint(0, T) for i in range(l)]
    u.sort()
    lset = set(u)
    return [x for x in lset]
